package net.loodies;

import android.graphics.Rect;

public class Transform {

	float orientationx = 0.0f;
	float orientationy = 0.0f;
	float orientationz = 0.0f;

	static float velocityx = 0.0f;
	static float velocityy = 0.0f;
//	private float3 mPosition = new float3(0, 0, 0);
	
	
	
	// --------------------------------------------
	// moveAccel
	// --------------------------------------------
	public static int2 moveAccel(float delta,Rect bound,int posx,int posy,boolean rotated,float gravity) {

		float gravity_factor = gravity;
		float accel_duration = 0.008f;
		float mps_to_pixel = delta * (0.10f);
		float friction = 0.4f; // 1-0.4/gravity; // 0.8

		

		float bound_Y_min = bound.top;
		float bound_X_min = bound.left;
		float bound_Y_max = bound.bottom;
		float bound_X_max = bound.right;
		float3 acceleration = new float3(AccelerometerManager.acceleration);
	
	/*	Log.v("Transform", "AccelerometerManager.acceleration" + AccelerometerManager.acceleration.toString());
		Log.v("Transform", "bound" + bound.toString());
		Log.v("Transform", "posx:" + posx+" posy:" + posy);
		*/	
		float2 fposition = new float2((float)posx,(float)posy);
		if (rotated == false) {
			acceleration.y=-acceleration.y;
		}
		if (rotated == true) {
			acceleration.x = AccelerometerManager.acceleration.y * -1;
			acceleration.y = AccelerometerManager.acceleration.x;
	
		}

		velocityx -= acceleration.x * accel_duration * gravity_factor;
		velocityy -= acceleration.y * accel_duration * gravity_factor;

		fposition.x += velocityx * mps_to_pixel;
		fposition.y += velocityy * mps_to_pixel;
		// if bounce we invert and reduce the direction

		if (fposition.y < bound_Y_min) {
			velocityy = -velocityy * friction;
			velocityx = velocityx * friction;
			fposition.y = bound_Y_min;

		}

		if (fposition.y > bound_Y_max) {
			velocityy = -velocityy * friction;
			velocityx = velocityx * friction;
			fposition.y = bound_Y_max;

		}

		if (fposition.x < bound_X_min) {
			velocityx = -velocityx * friction;
			velocityy = velocityy * friction;
			fposition.x = bound_X_min;

		}

		if (fposition.x > bound_X_max) {
			velocityx = -velocityx * friction;
			velocityy = velocityy * friction;
			fposition.x = bound_X_max;

		}
		
		return new int2((int)fposition.x,(int)fposition.y);
	}

}
